import { getToken, setToken, removeToken } from "@/utils/auth"
import { login } from "@/api/user"
const state = {
  token:getToken()
}

const mutations = {
  setToken(state, token) {
    // 保存到vuex
    state.token = token
    //  保存到本地
    setToken(token)
  }
}

const actions = {
  async doLogin(next,val) {
    try {
      let res = await login(val)
      next.commit('setToken',res.data)
    } catch (error){
        return Promise.reject(error)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

